<template>
	<view class="charts-box">
		<qiun-data-charts type="pie" :opts="opts" :chartData="chartData" />
	</view>
</template>

<script>
	export default {
		props: {
			params: {
				type: Object,
				default: null
			}
		},
		data() {
			return {
				chartData: {},
				opts: {
					color: ["#1890FF", "#91CB74", "#FAC858", "#EE6666", "#73C0DE", "#3CA272", "#FC8452", "#9A60B4",
						"#ea7ccc"
					],
					padding: [5, 5, 5, 5],
					enableScroll: false,
					extra: {
						pie: {
							activeOpacity: 0.5,
							activeRadius: 10,
							offsetAngle: 0,
							labelWidth: 15,
							border: false,
							borderWidth: 3,
							borderColor: "#FFFFFF"
						}
					}
				}
			};
		},
		created() {
			this.getServerData();
		},
		watch: {
			params(value) {
				this.params = value;
				this.getServerData();
			}
		},
		methods: {
			getServerData() {
				this.$api({
					method: 'post',
					url: '/system/billing/statistic',
					data: this.params
				}).then(res => {
					if (res.code == 0) {
						let list = []
						res.rows.forEach(data => {
							list.push({
								name: data.name,
								value: Number(data.amount),
								show: true
							})
						});
						let resouce = {
							series: [{
								data: list
							}]
						};
						this.chartData = resouce;
					} else {
						this.$u.toast(res.msg);
					}
				}).catch(err => {
					this.$u.toast("网络连接失败");
				});
			},
		}
	};
</script>

<style scoped>
	/* 请根据实际需求修改父元素尺寸，组件自动识别宽高 */
	.charts-box {
		width: 100%;
		height: 300px;
	}
</style>